package com.v1.weibo4j;

import java.util.Properties;

import javax.annotation.Resource;

import org.apache.struts.action.ActionForward;

import weibo4j.Oauth;
import weibo4j.model.WeiboException;
import weibo4j.util.WeiboConfig;

import com.v1.util.Constant;
import com.v1.util.LifeCycle;

public class Weibo4jConfigAgent extends LifeCycle {
	@Resource(name = "Constant")
	private Constant constant;
	private ActionForward authorize;

	public void init() {
		Properties props = new Properties();
		props.setProperty("client_ID", constant.getString("client_ID"));
		props.setProperty("client_SERCRET", constant.getString("client_SERCRET"));
		props.setProperty("redirect_URI", constant.getString("redirect_URI"));
		props.setProperty("baseURL", constant.getString("baseURL"));
		props.setProperty("accessTokenURL", constant.getString("accessTokenURL"));
		props.setProperty("authorizeURL", constant.getString("authorizeURL"));
		WeiboConfig.setProps(props);

		Oauth oauth = new Oauth();
		try {
			String path = oauth.authorize("code");
			authorize = new ActionForward(path, true);
		} catch (WeiboException e) {
			throw new NullPointerException("weibo authorize url is null.");
		}

	}

	@Override
	public void destroy() {
		WeiboConfig.setProps(null);
		authorize = null;
	}

	public ActionForward getAuthorize() {
		return authorize;
	}
}
